package com.aiti.lulian.controller;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.LuLianEnterpriseOperationDto;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.crad.LuLianEnterpriseOperation;
import com.aiti.lulian.service.ILuLianEnterpriseOperationService;
import com.aiti.lulian.service.ILuLianEnterpriseService;
import com.aiti.lulian.vo.LuLianEnterpriseOperationVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Api(tags = "企业运营控制层")
@RestController
@RequestMapping("/enterprise/operation")
public class LuLianEnterpriseOperationController extends WebController {

    private final ILuLianEnterpriseOperationService luLianEnterpriseOperationService;

    private final ILuLianEnterpriseService luLianEnterpriseService;

    public LuLianEnterpriseOperationController(ILuLianEnterpriseOperationService luLianEnterpriseOperationService, ILuLianEnterpriseService luLianEnterpriseService) {
        this.luLianEnterpriseOperationService = luLianEnterpriseOperationService;
        this.luLianEnterpriseService = luLianEnterpriseService;
    }

    @ApiOperation("编辑详情")
    @PostMapping("/saveOrUpdate")
    public Message saveOrUpdateEnterpriseOperation(@RequestBody LuLianEnterpriseOperation luLianEnterpriseOperation){
        String userId = findUserId();
//        String userId = "1";
        if (StrUtil.isBlank(userId)) {
            return Fail("请先登录");
        }
        luLianEnterpriseOperation.setCreator(userId);
        try {
            luLianEnterpriseOperationService.saveOrUpdateEnterpriseOperation(luLianEnterpriseOperation);
            return Message.success("操作成功");
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Message.fail("操作失败");
        }
    }

    @ApiOperation("查询详情")
    @GetMapping("/getDetails")
    public Message getEnterpriseOperationDetails(@RequestParam String enterpriseId){
        String userId = findUserId();
//        String userId = "1";
        if (StrUtil.isBlank(userId)) {
            return Fail("请先登录");
        }
        return luLianEnterpriseOperationService.getEnterpriseOperationDetails(enterpriseId);
    }

    @ApiOperation("查询企业列表")
    @GetMapping("/getList")
    public Message getEnterpriseList(LuLianEnterpriseOperationDto luLianEnterpriseOperationDto){
//        String userId = findUserId();
        String userId = "3d611d48853f11ea9e870221860e9b7e";
        if (StrUtil.isBlank(userId)) {
            return Fail("请先登录");
        }
        luLianEnterpriseOperationDto.setUserId(userId);
        Integer pageNum = luLianEnterpriseOperationDto.getPageNum();
        Integer pageSize = luLianEnterpriseOperationDto.getPageSize();
        Page<LuLianEnterprise> page = startPage(pageNum, pageSize);
        List<LuLianEnterpriseOperationVo> vos = luLianEnterpriseService.getEnterpriseList(luLianEnterpriseOperationDto, page);
        return Success(getPageResult(vos, pageNum, pageSize, page.getTotal()));
    }

    @ApiOperation("导出企业列表")
    @GetMapping("/exportList")
    public void exportList(HttpServletResponse response , LuLianEnterpriseOperationDto luLianEnterpriseOperationDto) {
        String userId = findUserId();
        // 测试用的账号
        // userId = "3d611d48853f11ea9e870221860e9b7e";
        luLianEnterpriseOperationDto.setUserId(userId);
        luLianEnterpriseService.exportExcel(luLianEnterpriseOperationDto, userId, response);
    }

    @ApiOperation("查询运营数据")
    @GetMapping("/getData")
    public Message getData(){
        String userId = findUserId();
//        String userId = "3d611d48853f11ea9e870221860e9b7e";
        if (StrUtil.isBlank(userId)) {
            return Fail("请先登录");
        }
        return luLianEnterpriseOperationService.getData();
    }

    @ApiOperation("查询企业资质认定图片")
    @GetMapping("/getQualificationRecognitionPic")
    public Message getQualificationRecognitionPic(@RequestParam("enterpriseId") String enterpriseId){
        LuLianEnterpriseOperation enterpriseOperation = luLianEnterpriseOperationService.getOne(new LambdaQueryWrapper<LuLianEnterpriseOperation>().eq(LuLianEnterpriseOperation::getEnterpriseId, enterpriseId));
        if (enterpriseOperation != null) {
            String qualificationRecognitionPic = enterpriseOperation.getQualificationRecognitionPic();
            if (StrUtil.isNotBlank(qualificationRecognitionPic)) {
                return Message.success(qualificationRecognitionPic);
            } else {
                return Message.fail("尚未上传资质认定图片");
            }
        }
        return Message.fail("查询失败");
    }

    /**
     * 新版名片小程序
     */
    @ApiOperation("查询企业资质认定图片")
    @GetMapping("/getQualificationRecognitionPicNew")
    public Message getQualificationRecognitionPicNew(@RequestParam("enterpriseId") String enterpriseId){
        LuLianEnterprise enterprise = luLianEnterpriseService.getById(enterpriseId);
        if (enterprise == null) {
            return Message.fail("企业信息失效");
        }
        String qualificationRecognitionPic = enterprise.getQualificationRecognitionPic();
        if (StrUtil.isNotBlank(qualificationRecognitionPic)) {
            return Message.success(qualificationRecognitionPic);
        } else {
            return Message.fail("尚未上传资质认定图片");
        }
    }

}